package com.rui.cn.provider.controller.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.rui.cn.entity.system.SysMenu;
import com.rui.cn.entity.system.SysPermission;
import com.rui.cn.provider.service.system.SysPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/syspermis")
public class SysPermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;

    @GetMapping(value = "/loadUserPermission/{id}")
    public List<SysPermission> loadUserPermission(@PathVariable("id") Integer id){
        return  sysPermissionService.loadUserPermission(id);
    }

    @PostMapping(value = "/permissionList")
    public List<SysPermission> permissionList(@RequestBody Pagination pagination,@RequestParam(required =false,name = "search") String search,@RequestParam(required =false,name = "sortBy") String sortBy,@RequestParam(required =false,name = "order") String order){
        return  sysPermissionService.permissionList(pagination,search,sortBy,order);
    }

    @GetMapping(value = "/getRolePermiss/{roleid}")
   public List<SysPermission> getRolePermiss(@PathVariable("roleid") Integer roleid){
        return sysPermissionService.getRolePermiss(roleid);
    }

    @GetMapping(value = "/getPermissionInfo/{id}")
    public SysPermission getPermissionInfo(@PathVariable("id") Integer id){
        return  sysPermissionService.getPermissionInfo(id);
    }

    @PostMapping(value = "/selectList")
    public List<SysPermission> selectList(){
        return  sysPermissionService.selectList(new Wrapper<SysPermission>() {
            @Override
            public String getSqlSegment() {
                return null;
            }
        });
    }

    @PostMapping(value = "/insertOrUpdate")
    public boolean insertOrUpdate(@RequestBody SysPermission permission){
        return  sysPermissionService.MyinsertOrUpdate(permission);
    }

    @DeleteMapping(value = "/deleteBatchIds")
    public boolean deleteBatchIds(@RequestBody List<Integer> integers){
        return sysPermissionService.deleteBatchIds(integers);
    }

    @GetMapping(value = "/selectById/{id}")
    public SysPermission selectById(@PathVariable("id") Integer id){
        return  sysPermissionService.selectById(id);
    }

    @GetMapping(value = "/selectByIdOrParentId/{id}")
    public List<SysPermission> selectByIdOrParentId(@PathVariable("id") String id){
        EntityWrapper<SysPermission> ew = new EntityWrapper<SysPermission>();
        ew.setEntity(new SysPermission());
        if(StringUtils.isNotBlank(id) && !id.equals("0")){
            ew.where("id={0}",id).or("parentid={0}",id);
        }
        return sysPermissionService.selectList(ew);
    }
    @GetMapping(value = "/selectByParentId/{parentid}")
    List<SysPermission> selectByParentId(@PathVariable("parentid") Integer parentid){
        return sysPermissionService.selectList(new Wrapper<SysPermission>() {
            @Override
            public String getSqlSegment() {
                return "where parentid="+parentid;
            }
        });
    }
}
